package com.honey.compilation.sql.dml;

import java.util.ArrayList;
import java.util.List;

import com.honey.compilation.sql.SqlColumn;
import com.honey.compilation.sql.SqlKeyWord;
import com.honey.compilation.sql.SqlScript;
import com.honey.compilation.sql.SqlTable;
import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.IndentSpace;

/**
 * 修改命令 Update
 * @author Administrator
 *
 */
public  class Update extends AbstractUpdate implements SqlScript{
	
	/** 数据库表 */
	private SqlTable table;
	
	/** 数据库列 */
	private List<SqlColumn> columns ;
	
	/** 修改的值 */
	private List<String> values ;
	
	/** 条件 */
	private Condition condition ;
	
	/**
	 * 构造函数
	 * @param tableName 表
	 */
	public Update(String tableName){
		this(new SqlTable(tableName));
	}
	
	/**
	 * 构造函数
	 * @param schema 数据库结构
	 * @param tableName 表名称
	 */
	public Update(String schema ,String tableName){
		this(new SqlTable(schema,tableName));
	}
	
	/**
	 * 构造函数
	 * @param table 数据库
	 */
	public Update(SqlTable table){
		this.table = table;
		columns = new ArrayList<SqlColumn>();
		values = new ArrayList<String>();
	}
	
	/**
	 * 构造函数
	 * @param table 数据库
	 * @param condition 条件
	 */
	public Update(SqlTable table,Condition condition ){
		this(table);
		this.condition = condition;
	}
	
	/**
	 * 设定数据库结构
	 * @param schema
	 */
	public void setSchema(String schema){
		this.table.setSchema(schema);
	}
	
	/**
	 * 获取数据结构
	 * @return
	 */
	public String getSchema(){
		return this.table.getSchema();
	}
	
	/**
	 * 获取数据库名称
	 * @return
	 */
	public String getTableName(){
		return this.table.getTableName();
	}
	
	/**
	 * 条件
	 * @return
	 */
	public Condition getCondition() {
		return condition;
	}
	
	/**
	 * 条件
	 * @param condition
	 */
	public void setCondition(Condition condition) {
		this.condition = condition;
	}
	
	/**
	 * 添加插入列,插入列要包含数据类型,否则按照字符处理.
	 * @param columns
	 */
	@Deprecated
	public Update addColumn(SqlColumn ...columns ){
		for(SqlColumn column : columns ){
			if( column != null )
				this.columns.add(column);
		}
		return this;
	}
	
	/**
	 * 添加插入值,值顺序和列的顺序要一致
	 * @param values
	 */
	@Deprecated
	public Update addValue(String ...values ){
		for(String value : values ){
			if( values != null )
				this.values.add(value);
		}
		return this;
	}
	
	
	/**
	 * 添加需要插入的数据列
	 * @param column 列
	 * @param value 插入值
	 */
	public Update addUpdateColumn(SqlColumn column,  String value){
		if(column != null && value != null){
			this.columns.add(column);
			this.values.add(value);
		}

		return this;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.core.dom.sql.dml.AbstractUpdate#compiledContent(int)
	 */
	@Override
	public CharacterBuilder compiledContent(int indentLevel ) throws CompilationException {
		CharacterBuilder answer = CharacterBuilderFactory.createC16StringBuilder();
		int columnsSize = columns.size();
		int valueSize = values.size();
		if(valueSize > 0 && columnsSize > 0){
			answer.append(super.compiledContent(indentLevel))
			.append(table.compiledContent(0));
			IndentSpace.newSpace(answer, indentLevel);
			answer.append(SqlKeyWord.SET);
			boolean comma = false;
			for( int i=0;i < columnsSize;i++ ){
				if (comma){answer.append(SqlKeyWord.OPERATION_COMMA);}else comma = true;
				
				IndentSpace.newSpace(answer, indentLevel+1);
				SqlColumn c = columns.get(i);
				answer.append(c.getName()).append(SqlKeyWord.OPERATION_EQUALS);
				boolean b = !c.isNumber();
				if( i < valueSize){
					if( b )answer.append(SqlKeyWord.OPERATION_SINGLE_QUOTES);
					answer.append(values.get(i));
					if( b )answer.append(SqlKeyWord.OPERATION_SINGLE_QUOTES);
					
				}else{
					answer.append("NULL");
				}				
			}

			if(condition != null){
				condition.setEnableAlias(false);
				answer.append(SqlKeyWord.OPERATION_SPACE);
				IndentSpace.newSpace(answer, indentLevel);
				answer.append(SqlKeyWord.WHERE);
				IndentSpace.newSpace(answer, indentLevel+1);
				answer.append(condition.compiledContent(indentLevel))
				;
			}
			answer.append(SqlKeyWord.OPERATION_SEMICOLON);
		}
		return answer;
	}
}
